package com.koron.css2.meterService.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MeterBoreBean;
import com.koron.css2.baseConfig.bean.MeterFactoryBean;
import com.koron.css2.baseConfig.bean.MeterModelBean;
import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.baseConfig.mapper.MeterFactoryMapper;
import com.koron.css2.baseConfig.mapper.MeterModelMapper;
import com.koron.css2.meterService.bean.query.MsRepairQueryBean;
import com.koron.css2.meterService.dto.MsRepairDto;
import com.koron.css2.meterService.mapper.MsRepairMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

/**
 * 维检列表查询
 * @author 周利斌
 *
 */
public class MsRepairList implements ServerInterface{
	
	private static Logger log = LogManager.getLogger(MsRepairList.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);
		try {
			MsRepairQueryBean bean = JsonUtils.objectToPojo(req.getData(), MsRepairQueryBean.class);
			
			MsRepairMapper mapper = factory.getMapper(MsRepairMapper.class);
			MeterFactoryMapper meterFactoryMapper = factory.getMapper(MeterFactoryMapper.class);

			PageHelper.startPage(bean.getPage(), bean.getPageCount());
			List<MsRepairDto> list = mapper.selectMsRepairList(bean);
			Map<String, String> sources = Tools.mapDicByCode(factory, "PIS");  //信息来源
			Map<String, String> repairTypes = Tools.mapDicByCode(factory, "REPAIR_TYPE");  //维检类型
			Map<String, String> calibrationResults = Tools.mapDicByCode(factory, "CALI_RESULT"); //检定结果
			list.stream().forEach(t -> {
				Map<String, String> meterTypes = Tools.mapDicByCode(factory, "MMT");
				if(t.getSourceFlag() != null) {
					t.setSourceFlagName(sources.get(t.getSourceFlag()));
				}
				//水表厂商
		        MeterFactoryBean factoryBean =  meterFactoryMapper.selectMeterFactoryById(t.getMeterFactory());     
				// 水表厂家
				t.setMeterFactory(factoryBean==null?t.getMeterFactory():factoryBean.getFactoryName());
				//水表型号
				MeterModelMapper meterModelMapper = factory.getMapper(MeterModelMapper.class);
				MeterModelBean modelBean = meterModelMapper.selectMeterModelById(t.getMeterModel());
				t.setMeterModel(modelBean ==null?t.getMeterModel():modelBean.getModelName());
				
				if (StringUtils.isNotBlank(t.getMeterType())) {
					// 水表类型
					t.setMeterType(meterTypes.get(t.getMeterType()));
				}
				
				MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class);
				MeterBoreBean boreBean = meterBoreMapper.selectMeterBoreById(t.getMeterBore());
				// 水表口径
				t.setMeterBore(boreBean==null?t.getMeterBore():boreBean.getBoreName());
				
				if(StringUtils.isNotBlank(t.getRepairType())) {
					t.setRepairTypeName(repairTypes.get(t.getRepairType()));
				}
				if(t.getCalibrationResult() != null) {
					t.setCalibrationResultName(calibrationResults.get(t.getCalibrationResult()));
				}
//				if(StringUtils.isNotBlank(t.getMeterFactory())) {
//					Optional<MeterFactoryVO>  optional = meterFactorys.stream().filter(o -> Objects.equals(o.getId(), t.getMeterFactory())).findFirst();
//					t.setMeterFactory(optional.orElse(new MeterFactoryVO()).getFactoryName());
//				}
				t.setProcessStateName(Tools.transProcessStatus(t.getProcessState()));
			});
			info.setData(new PageInfo<MsRepairDto>(list));
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("查询失败");
			log.error(e.getMessage(), e);
		}
		return info;
	}
	
}
